package com.atguigu.mr.sort;

import com.atguigu.mr.flow.FlowBean;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;

/**
 * @description: 要求每个省份手机号输出的文件中按照总流量内部排序
 * @time: 20191226 20:08
 * @author: baojinlong
 **/
public class ProvincePartitioner extends Partitioner<FlowBean, Text> {

    @Override
    public int getPartition(FlowBean key, Text value, int numPartitions) {
        // 1 获取手机号码前三位
        String preNum = value.toString().substring(0, 3);
        int partition = 4;
        // 2 根据手机号归属地设置分区
        switch (preNum) {
            case "136":
                partition = 0;
                break;
            case "137":
                partition = 1;
                break;
            case "138":
                partition = 2;
                break;
            case "139":
                partition = 3;
                break;
            default:
                break;
        }
        return partition;
    }
}
